<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Interactive 9 Grid Charts</title>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <style>
    body {
      font-family: 'Arial', sans-serif;
      margin: 0;
      padding: 20px;
      background-color: #f8f9fa;
    }
    .dashboard-title {
      text-align: center;
      margin-bottom: 20px;
      color: #333;
    }
    .grid-container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(3, 1fr);
      gap: 15px;
      max-width: 1200px;
      margin: 0 auto;
    }
    .chart-container {
      background-color: white;
      border-radius: 10px;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      padding: 15px;
      transition: transform 0.3s ease, box-shadow 0.3s ease;
      height: 300px;
    }
    .chart-container:hover {
      transform: translateY(-5px);
      box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
    }
    .chart-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
    }
    .chart-title {
      margin: 0;
      font-size: 16px;
      color: #333;
    }
    .chart-canvas {
      width: 100% !important;
      height: 70% !important;
    }
    .chart-legend {
      display: flex;
      justify-content: center;
      margin-top: 10px;
    }
    .legend-item {
      display: flex;
      align-items: center;
      margin: 0 5px;
      font-size: 12px;
    }
    .legend-color {
      width: 12px;
      height: 12px;
      margin-right: 5px;
      border-radius: 2px;
    }
    .refresh-button {
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 4px;
      padding: 5px 10px;
      cursor: pointer;
      font-size: 12px;
      transition: background-color 0.3s ease;
    }
    .refresh-button:hover {
      background-color: #0056b3;
    }
  </style>
</head>
<body>
  <h1 class="dashboard-title">Interactive Data Dashboard</h1>
  <div class="grid-container">
    <div class="chart-container" id="chart1">
      <div class="chart-header">
        <h2 class="chart-title">Sales Trends</h2>
        <button class="refresh-button" onclick="refreshChart(0)">Refresh</button>
      </div>
      <canvas id="canvas1" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend1"></div>
    </div>
    <div class="chart-container" id="chart2">
      <div class="chart-header">
        <h2 class="chart-title">Revenue Growth</h2>
        <button class="refresh-button" onclick="refreshChart(1)">Refresh</button>
      </div>
      <canvas id="canvas2" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend2"></div>
    </div>
    <div class="chart-container" id="chart3">
      <div class="chart-header">
        <h2 class="chart-title">Market Share</h2>
        <button class="refresh-button" onclick="refreshChart(2)">Refresh</button>
      </div>
      <canvas id="canvas3" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend3"></div>
    </div>
    <div class="chart-container" id="chart4">
      <div class="chart-header">
        <h2 class="chart-title">Customer Demographics</h2>
        <button class="refresh-button" onclick="refreshChart(3)">Refresh</button>
      </div>
      <canvas id="canvas4" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend4"></div>
    </div>
    <div class="chart-container" id="chart5">
      <div class="chart-header">
        <h2 class="chart-title">Visits Analytics</h2>
        <button class="refresh-button" onclick="refreshChart(4)">Refresh</button>
      </div>
      <canvas id="canvas5" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend5"></div>
    </div>
    <div class="chart-container" id="chart6">
      <div class="chart-header">
        <h2 class="chart-title">Views Over Time</h2>
        <button class="refresh-button" onclick="refreshChart(5)">Refresh</button>
      </div>
      <canvas id="canvas6" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend6"></div>
    </div>
    <div class="chart-container" id="chart7">
      <div class="chart-header">
        <h2 class="chart-title">Device Usage</h2>
        <button class="refresh-button" onclick="refreshChart(6)">Refresh</button>
      </div>
      <canvas id="canvas7" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend7"></div>
    </div>
    <div class="chart-container" id="chart8">
      <div class="chart-header">
        <h2 class="chart-title">Platform Distribution</h2>
        <button class="refresh-button" onclick="refreshChart(7)">Refresh</button>
      </div>
      <canvas id="canvas8" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend8"></div>
    </div>
    <div class="chart-container" id="chart9">
      <div class="chart-header">
        <h2 class="chart-title">Quarterly Profit</h2>
        <button class="refresh-button" onclick="refreshChart(8)">Refresh</button>
      </div>
      <canvas id="canvas9" class="chart-canvas"></canvas>
      <div class="chart-legend" id="legend9"></div>
    </div>
  </div>
  <script>
    let charts = [];
    let chartData = [];

    async function fetchChartData() {
      try {
        const response = await fetch('http://localhost:10015/test2');
        const data = await response.json();
        return data;
      } catch (error) {
        console.error('Error fetching chart data:', error);
        return [];
      }
    }

    async function initCharts() {
      chartData = await fetchChartData();
      const canvasIds = ['canvas1', 'canvas2', 'canvas3', 'canvas4', 'canvas5', 'canvas6', 'canvas7', 'canvas8', 'canvas9'];
      const legendIds = ['legend1', 'legend2', 'legend3', 'legend4', 'legend5', 'legend6', 'legend7', 'legend8', 'legend9'];

      canvasIds.forEach((canvasId, index) => {
        const ctx = document.getElementById(canvasId).getContext('2d');
        const chartConfig = chartData[index];
        const legendContainer = document.getElementById(legendIds[index]);

        // Clear previous chart if it exists
        if (charts[index]) {
          charts[index].destroy();
        }

        // Create new chart
        const chart = new Chart(ctx, {
          type: chartConfig.type,
          data: chartConfig.data,
          options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
              tooltip: {
                mode: 'index',
                intersect: false,
              },
              legend: {
                display: false
              }
            },
            scales: {
              y: {
                beginAtZero: true
              }
            },
            animation: {
              duration: 1000,
              easing: 'easeOutQuart'
            }
          }
        });

        // Add custom legend
        const legendHTML = [];
        chartConfig.data.datasets.forEach((dataset, datasetIndex) => {
          const color = dataset.backgroundColor || dataset.borderColor;
          const label = dataset.label;
          legendHTML.push(`
            <div class="legend-item">
              <div class="legend-color" style="background-color: ${color}"></div>
              <span>${label}</span>
            </div>
          `);
        });
        legendContainer.innerHTML = legendHTML.join('');

        // Store the chart instance
        charts[index] = chart;
      });
    }

    function refreshChart(index) {
      if (index >= 0 && index < chartData.length) {
        const newDataset = generateRandomDataset(chartData[index].type, chartData[index].data.labels.length);
        chartData[index].data.datasets = [newDataset];
        charts[index].update();
      }
    }

    function generateRandomDataset(type, dataLength) {
      const colors = [
        'rgba(75, 192, 192, 0.6)',
        'rgba(153, 102, 255, 0.6)',
        'rgba(255, 159, 64, 0.6)',
        'rgba(54, 162, 235, 0.6)',
        'rgba(255, 99, 132, 0.6)'
      ];
      const borderColor = colors[Math.floor(Math.random() * colors.length)].replace('0.6', '1');
      const backgroundColor = colors[Math.floor(Math.random() * colors.length)];

      let data = [];
      for (let i = 0; i < dataLength; i++) {
        data.push(Math.floor(Math.random() * 100) + 10);
      }

      return {
        label: `Random Data ${Math.floor(Math.random() * 100)}`,
        data: data,
        borderColor: borderColor,
        backgroundColor: backgroundColor,
        fill: type === 'line' ? false : true
      };
    }

    window.addEventListener('load', initCharts);
    window.addEventListener('resize', () => {
      charts.forEach(chart => {
        if (chart) {
          chart.resize();
        }
      });
    });
  </script>
</body>
</html>